#include <cstdio>
#include <cstdlib>
#include <iostream>

int main (int argc, char* argv [])
{
    /*
     * Verify input data
     */
    if (argc < 4)
    {
        std::cerr << "Usage: " << argv [0] << " <delay> <D> <alpha>\n";
        return -1;
    }

    const int delay = atoi (argv [1]);
    const int d = atoi (argv [2]);
    const float alpha = atof (argv [3]);

    if ((alpha < 0) || (alpha >= 1.0))
    {
        std::cerr << "Alpha should be in the following range: (0 - 1.0)\n";
        return -1;
    }

    /*
     * Allocate input and output buffers
     */
    const int inputDataLength = delay;
    int* inputData = new int [inputDataLength];
    
    const int outputDataLength = delay * d;
    int* outputData = new int [outputDataLength];
    
     /*
      * Generate random input signal
      */
    for (int i = 0; i < inputDataLength; ++i)
    {
        const int sample = rand () % 100;

        inputData [i] = sample;
        outputData [i] = sample;
    }

    /*
     * Generate output data and send to stdout
     */
    for (int i = delay + 1; i < outputDataLength; ++i)
    {
        outputData [i] = alpha * outputData [i - delay];
    }

    for (int i = 0; i < outputDataLength; ++i)
    {
        const int sample = outputData [i];

        putc (sample >> 8, stdout);
        putc (sample, stdout);
    }

    delete [] inputData;
    delete [] outputData;

    /*
     * Print out small report 
     */
    std::cerr << "Procesing done:\n";
    std::cerr << "\tDelay:\t" << delay << std::endl;
    std::cerr << "\tD:\t" << d << std::endl;
    std::cerr << "\tAlpha:\t" << alpha << std::endl;
    std::cerr << "\tOutput:\t" << outputDataLength << " samples " << std::endl;

    return 0;
}

